#include <bits/stdc++.h>
#define re register
#define int long long

using namespace std;

const int N = 50;
int l,r;
int dp[N][2][2];

inline int read(){
    int r = 0,w = 1;
    char c = getchar();
    while (c < '0' || c > '9'){
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9'){
        r = (r << 3) + (r << 1) + (c ^ 48);
        c = getchar();
    }
    return r * w;
}

inline int calc(int x,int y){
    int res = 0;
    memset(dp,0,sizeof(dp)); dp[0][0][0] = 1;
    vector<int> A,B; A.push_back(0); B.push_back(0);
    for (re int i = 32;~i;i--) A.push_back((x >> i) & 1);
    for (re int i = 32;~i;i--) B.push_back((y >> i) & 1);
    // for (int x:A) cerr << x << " "; cerr << "\n";
    for (re int i = 1;i <= 33;i++){
        for (re int l0 = 0;l0 <= 1;l0++){
            for (re int l1 = 0;l1 <= 1;l1++){
                if (!dp[i - 1][l0][l1]) continue;
                int Max0 = (l0 ? 1 : A[i]),Max1 = (l1 ? 1 : B[i]);
                for (re int j = 0;j <= Max0;j++){
                    for (re int k = 0;k <= Max1;k++){
                        if (!j && k) continue;
                        dp[i][l0 | (j < A[i])][l1 | (k < B[i])] += dp[i - 1][l0][l1];
                        // cerr << i << " " << A[i] << " " << B[i] << " " << (l0 | (j < A[i])) << " " << (l1 | (k < B[i])) << " " << dp[i - 1][l0][l1] << " !!!\n";
                    }
                }
            }
        }
    }
    for (re int i = 0;i <= 1;i++){
        for (re int j = 0;j <= 1;j++) res += dp[33][i][j];
    } return res;
}

inline void solve(){
    int ans = 0;
    l = read(),r = read();
    // for (re int i = l;i <= r;i++){
    //     for (re int j = l;j <= i;j++){
    //         if ((i & j) == j) ans++;
    //     }
    // }
    printf("%lld\n",calc(r,r) - calc(l - 1,r) - calc(r,l - 1) + calc(l - 1,l - 1));
    // cerr << calc(r,r) << " ???\n";
}

signed main(){
    freopen("comb.in","r",stdin);
    freopen("comb.out","w",stdout);
    int T = read();
    while (T--) solve();
    return 0;
}

/*
太阴了，C(n,m) % 2 = [(n & m) = m]
直接数位 dp，记 dp[i][j][k][l0][l1] 
*/